package com.city.controller.grid;

import com.city.config.BaiduMap;
import com.city.config.Status;
import com.city.domain.city.CityTown;
import com.city.domain.report.CtEvent;
import com.city.domain.report.CtReport;
import com.city.domain.report.CtTask;
import com.city.format.CtReportFormat;
import com.city.format.CtReportFormatSql;
import com.city.helper.TimeStampHelper;
import com.city.repository.report.CtReportFormatSqlRepository;
import com.city.repository.report.CtReportRepository;
import com.city.service.city.CityCopnoteMaincontentService;
import com.city.service.city.CityCopnoteObjectService;
import com.city.service.report.CtTaskService;
import com.city.service.report.ReportService;
import com.city.helper.AuthHelper;
import com.city.helper.FormatHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * @deprecation 移动网格化深度应用
 * @email leichuqiao@126.com
 * @author 雷楚桥
 * @date 2018-07-06 11:21
 */
@Controller
@RequestMapping("/grid")
public class GridController {

    @Autowired
    private ReportService reportService;
    @Autowired
    private FormatHelper formatHelper;
    @Autowired
    private AuthHelper authHelper;
    @Autowired
    private CityCopnoteMaincontentService contentService;
    @Autowired
    private CityCopnoteObjectService objectService;
    @Autowired
    private CtReportFormatSqlRepository sqlRepository;
    @Autowired
    private CtTaskService ctTaskService;



    /**
     * @deprecation 系统主页
     * @author 雷楚桥
     * @date 2018-07-06 21:11
     */
    @RequestMapping("/index")
    public String index(Map<String,Object> map){
        return "/grid/index";
    }

    /**
     * @deprecation 民情日记登记上报
     * @author 雷楚桥
     * @date 2018-07-06 14:04
     */
    @RequestMapping("/diary")
    public String diary(Map<String,Object> map){
        //乡镇信息
        List<CityTown> townList = reportService.findAllCityTown();
        map.put("townList",townList);

        //村庄信息
        map.put("villageList",reportService.findCityVillageByTownId(townList.get(0).getTownId()));

        //主要对象
        map.put("mainObject",objectService.getAll());

        //主要内容
        map.put("mainContent",contentService.getAll());

        map.put("AK", BaiduMap.BAIDU_AK);
        map.put("titleName","民情日记登记上报");
        return "/grid/diary";
    }

    /**
     * @deprecation 基于GIS的事件登记上报
     * @author 雷楚桥
     * @date 2018-07-06 14:58
     */
    @RequestMapping("/gis")
    public String gis(Map<String,Object> map){
        //乡镇信息
        List<CityTown> townList = reportService.findAllCityTown();
        map.put("townList",townList);

        //村庄信息
        map.put("villageList",reportService.findCityVillageByTownId(townList.get(0).getTownId()));

        //事件类别
        List<CtEvent> eventList = reportService.findAllCtEventByCategoryIdAndParent(1,0);
        map.put("eventList",eventList);

        map.put("AK", BaiduMap.BAIDU_AK);
        map.put("titleName","基于GIS的事件登记上报");
        return "/grid/gis";
    }

    /**
     * @deprecation 地图浏览
     * @author 雷楚桥
     * @date 2018-07-06 14:59
     */
    @RequestMapping("/map")
    public String map(Map<String,Object> map){
        map.put("AK", BaiduMap.BAIDU_AK);
        map.put("titleName","地图浏览");
        return "/grid/map";
    }

    /**
     * @deprecation 任务查询列表
     * @author 雷楚桥
     * @date 2018-07-12 16:09
     */
    @RequestMapping("/task")
    public String list(Map<String,Object> map, @Param("page") Integer page, HttpSession session){

        // TODO 查询这个网格员的任务 taskList

        //每页显示的条数
        long pageSize = 15;

        long total = sqlRepository.countTasks(2,authHelper.thisBranchId());
        //查到记录总数
        map.put("reportNum", total);

        //总页数
        long pageTimes;
        if (total % pageSize == 0) {
            pageTimes = total / pageSize;
        } else {
            pageTimes = total / pageSize + 1;
        }
        map.put("pageTimes", pageTimes);
        session.setAttribute("pageTimes", pageTimes);

        //页面初始的时候page没有值
        if (null == page) {
            page = 1;
        }

        //每页开始的第几条记录
        long startRow = (page - 1) * pageSize;
        map.put("currentPage", page);
        List<CtReportFormatSql> ctReportFormatSqls = sqlRepository.findTasks(authHelper.thisBranchId(),2,startRow,pageSize);
        if(ctReportFormatSqls.size() !=0){
            ctReportFormatSqls.stream().forEach(ctReportFormatSql -> ctReportFormatSql.setCreateTimeName(new TimeStampHelper().stampToDate(ctReportFormatSql.getCreateTimeName())));
            map.put("reportList",ctReportFormatSqls);
        }else {
            map.put("reportList",null);
        }

        map.put("currentPage", page);
        map.put("titleName","任务查询列表");
        return "/grid/task";
    }

    /**
     * @deprecation 乡镇村社区自行处理处置重要事件上报
     * @author 雷楚桥
     * @date 2018-07-12 19:16
     */
    @RequestMapping("/self")
    public String self(Map<String, Object> map) {

        //乡镇信息
        List<CityTown> townList = reportService.findAllCityTown();
        map.put("townList",townList);

        //村庄信息
        map.put("villageList",reportService.findCityVillageByTownId(townList.get(0).getTownId()));

        //事件类别
        List<CtEvent> eventList = reportService.findAllCtEventByCategoryIdAndParent(1,0);
        map.put("eventList",eventList);

        map.put("AK", BaiduMap.BAIDU_AK);
        map.put("titleName","乡镇村社区自行处理处置重要事件上报");
        return "/grid/self";
    }

    /**
     * @deprecation 矛盾纠纷排查上报
     * @author 雷楚桥
     * @date 2018-07-12 19:40
     */
    @RequestMapping("/dispute")
    public String dispute(Map<String,Object> map){

        //乡镇信息
        List<CityTown> townList = reportService.findAllCityTown();
        map.put("townList",townList);

        //村庄信息
        map.put("villageList",reportService.findCityVillageByTownId(townList.get(0).getTownId()));

        //事件类别
        List<CtEvent> eventList = reportService.findAllCtEventByCategoryIdAndParent(2,0);

        map.put("AK", BaiduMap.BAIDU_AK);
        map.put("eventList",eventList);
        map.put("titleName","矛盾纠纷排查上报");
        return "/grid/dispute";
    }

    /**
     * @deprecation 自然灾害核查上报
     * @author 雷楚桥
     * @date 2018-07-12 20:08
     */
    @RequestMapping("/disaster")
    public String disaster(Map<String,Object> map){
        //乡镇信息
        List<CityTown> townList = reportService.findAllCityTown();
        map.put("townList",townList);

        //村庄信息
        map.put("villageList",reportService.findCityVillageByTownId(townList.get(0).getTownId()));

        //事件类别
        List<CtEvent> eventList = reportService.findAllCtEventByCategoryIdAndParent(3,0);
        map.put("eventList",eventList);

        map.put("AK", BaiduMap.BAIDU_AK);
        map.put("titleName","自然灾害核查上报");
        return "/grid/disaster";
    }
    
    /**
     * @deprecation 惠民资金监管
     * @author 雷楚桥
     * @date 2018-07-12 20:21
     */
    @RequestMapping("/capital")
    public String capital(Map<String,Object> map){
        //乡镇信息
        List<CityTown> townList = reportService.findAllCityTown();
        map.put("townList",townList);

        //村庄信息
        map.put("villageList",reportService.findCityVillageByTownId(townList.get(0).getTownId()));

        //事件类别
        List<CtEvent> eventList = reportService.findAllCtEventByCategoryIdAndParent(4,0);
        map.put("eventList",eventList);

        map.put("AK", BaiduMap.BAIDU_AK);
        map.put("titleName","惠民资金监管");
        return "/grid/capital";
    }

    /**
     * @deprecation 应急事件上报
     * @author 雷楚桥
     * @date 2018-07-12 20:33
     */
    @RequestMapping("/emergency")
    public String emergency(Map<String,Object> map){
        //乡镇信息
        List<CityTown> townList = reportService.findAllCityTown();
        map.put("townList",townList);

        //村庄信息
        map.put("villageList",reportService.findCityVillageByTownId(townList.get(0).getTownId()));

        //事件类别
        List<CtEvent> eventList = reportService.findAllCtEventByCategoryIdAndParent(5,0);
        map.put("eventList",eventList);

        map.put("AK", BaiduMap.BAIDU_AK);
        map.put("titleName","应急事件上报");
        return "/grid/emergency";
    }

    /**
     * @deprecation 扶贫信息上报
     * @author 雷楚桥
     * @date 2018-07-12 20:38
     */
    @RequestMapping("/poor")
    public String poor(Map<String,Object> map){
        //乡镇信息
        List<CityTown> townList = reportService.findAllCityTown();
        map.put("townList",townList);

        //村庄信息
        map.put("villageList",reportService.findCityVillageByTownId(townList.get(0).getTownId()));

        map.put("titleName","扶贫信息上报");
        return "/grid/poor";
    }
    
    /**
     * @deprecation 人口、法人、地理空间、农地林地、建筑物以及部件的数据采集与上报
     * @author 雷楚桥
     * @date 2018-07-12 20:50
     */
    @RequestMapping("/data")
    public String data(Map<String,Object> map){
        map.put("titleName","数据采集与上报");
        return "/grid/data";
    }

    /**
     * @deprecation 标准规范
     * @author 雷楚桥
     * @date 2018-07-13 10:36
     */
    @RequestMapping("/norm")
    public String norm(Map<String,Object> map){
        map.put("titleName","标准规范");
        return "/grid/norm";
    }

    /**
     * @deprecation 任务处置
     * @author 雷楚桥
     * @date 2018-07-13 13:27
     */
    @RequestMapping("/handle")
    public String handle(Map<String,Object> map,@Param("page") Integer page,HttpSession session){

        // TODO 查询这个网格员的任务 taskList


        //每页显示的条数
        long pageSize = 15;

        long total = sqlRepository.countMyTasks(3,authHelper.thisUserId());
        //查到记录总数
        map.put("reportNum", total);

        //总页数
        long pageTimes;
        if (total % pageSize == 0) {
            pageTimes = total / pageSize;
        } else {
            pageTimes = total / pageSize + 1;
        }
        map.put("pageTimes", pageTimes);
        session.setAttribute("pageTimes", pageTimes);

        //页面初始的时候page没有值
        if (null == page) {
            page = 1;
        }

        //每页开始的第几条记录
        long startRow = (page - 1) * pageSize;
        map.put("currentPage", page);
        List<CtReportFormatSql> ctReportFormatSqls = sqlRepository.findMyTasks(authHelper.thisUserId(),3,startRow,pageSize);
        if(ctReportFormatSqls.size() !=0){
            ctReportFormatSqls.stream().forEach(ctReportFormatSql -> ctReportFormatSql.setCreateTimeName(new TimeStampHelper().stampToDate(ctReportFormatSql.getCreateTimeName())));
            map.put("reportList",ctReportFormatSqls);
        }else {
            map.put("reportList",null);
        }


        map.put("currentPage", page);

        map.put("titleName","任务处置");
        return "/grid/handle";
    }

    /**
     * @deprecation 历史记录
     * @author 雷楚桥
     * @date 2018-07-13 13:28
     */
    @RequestMapping("/history")
    public String history(Map<String,Object> map,@Param("page") Integer page,HttpSession session){
        //每页显示的条数
        long pageSize = 15;

        long total = sqlRepository.countReportByLeft(-1, 7,authHelper.thisBranchId());
        //查到记录总数
        map.put("reportNum", total);

        //总页数
        long pageTimes;
        if (total % pageSize == 0) {
            pageTimes = total / pageSize;
        } else {
            pageTimes = total / pageSize + 1;
        }
        map.put("pageTimes", pageTimes);
        session.setAttribute("pageTimes", pageTimes);

        //页面初始的时候page没有值
        if (null == page) {
            page = 1;
        }

        //每页开始的第几条记录
        long startRow = (page - 1) * pageSize;



        //查询上报的历史记录
        List<CtReportFormatSql> sqls = sqlRepository.findDiaryByBranch(-1,7,authHelper.thisBranchId(), startRow, pageSize);
        sqls.stream().forEach(ctReportFormatSql -> ctReportFormatSql.setScheduleName(Status.REPORT_SCHEDULE.get(ctReportFormatSql.getSchedule())));
        sqls.stream().forEach(ctReportFormatSql -> ctReportFormatSql.setCreateTimeName(new TimeStampHelper().stampToDate(ctReportFormatSql.getCreateTimeName())));

        map.put("reportList", sqls);
        map.put("currentPage", page);
        map.put("titleName","历史记录");
        return "/grid/history";
    }

    /**
     * @deprecation 社情民意详情
     * @param id Integer id类型
     * @return String
     * @author 雷楚桥
     * @date 18-7-17 下午3:48
     */
    @RequestMapping("/detail")
    public String detail(@Param("id") Integer id,
                         Map<String,Object> map){

        CtReport ctReport = reportService.findReportById(id);

        String taskId = ctReport.getTaskId();
        //获取时间

        map.put("taskId", taskId);
        map.put("endTime",ctReport.getDeadline());

        //获取时间
        CtTask ctTask = ctTaskService.findByTaskId(id);

        map.put("taskTime", ctTask);
        map.put("reportFormat",formatHelper.ctReportFormat(ctReport));
        map.put("AK", BaiduMap.BAIDU_AK);
        map.put("titleName", "社情民意详情");
        return "/grid/detail";
    }


    /**
     * @author  王宇
     * @date 2018/9/20
     * @description
     */
    @RequestMapping("/otherproblem")
    public String otherproblem(Map<String,Object> map){
        //乡镇信息
        List<CityTown> townList = reportService.findAllCityTown();

        map.put("townList",townList);

        //村庄信息
        map.put("villageList",reportService.findCityVillageByTownId(townList.get(0).getTownId()));

        //事件类别
        List<CtEvent> eventList = reportService.findAllCtEventByCategoryIdAndParent(7,0);
        map.put("eventList",eventList);

        map.put("AK", BaiduMap.BAIDU_AK);
        map.put("titleName","其他问题录入");
        return "/grid/emergency";
    }
}
